package graphics.datastructures;

import graphics.raytracing.CastResult;
import graphics.raytracing.Ray;
import graphics.raytracing.Tracable;

import java.util.ArrayList;
import java.util.Collection;

public class SimpleModel<T extends Tracable<? extends Tracable<T>>> implements Model<T> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4244855250535314679L;
	Collection<T> _triangles = new ArrayList<T>();
	private SimpleMesh<T> mesh;
	private float[][] _boundingbox;
	BoundingBox<T> box;

	public CastResult<T> cast(Ray ray) {
		//return mesh.cast(ray);
		if (box.intersects(ray))
			return mesh.cast(ray);
		else
			return null;
	}

	public boolean equals(Object obj) {
		return mesh.equals(obj);
	}

	public float[][] getBB() {
		return _boundingbox;
	}

	public int hashCode() {
		return mesh.hashCode();
	}

	public String toString() {
		return mesh.toString();
	}

	public SimpleModel(Collection<T> tries) {
		mesh = new SimpleMesh<T>();
		for (T t : tries) {
			mesh.add(t);
		}
		float[][] t = mesh.getBB();
		box = new BoundingBox<T>(t[0], t[1]);
	}

	@Override
	public Collection<T> getTracables() {
		return mesh.getTracables();
	}

	@Override
	public BoundingBox getBoundingBox() {
		return box;
	}

}
